package com.nextbus.main;

import com.nextbus.provider.MySuggestionProvider;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceScreen;
import android.provider.SearchRecentSuggestions;
import android.util.Log;
import android.widget.Toast;

/**
 * 设置里面的参数通过Preferences来传递出来
 * @author cxd
 *
 */
public class Preferences extends PreferenceActivity {
    final static String TAG = "Preferences";
    SearchRecentSuggestions suggestions;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
        //声明suggestions用于清除搜索框历史记录
        suggestions = new SearchRecentSuggestions(
                this, MySuggestionProvider.AUTHORITY,
                MySuggestionProvider.MODE);
    }

    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,Preference preference) {
        super.onPreferenceTreeClick(preferenceScreen, preference);
        String key = preference.getKey();
        Log.d(TAG, "key = " + key);

        if (key.equals("runInBackgroundKey")) {
            /**
             * maybe invoke handler.sendMessage(key)
             *       handler invoke updateAsyncTask to update due to key
             */
        } else if (key.equals("showUnpredictableBusesKey")) {

        } else if (key.equals("hideCircleCheckboxKey")) {

        } else if (key.equals("showRouteLine")) {

        } else if (key.equals("clearHistory")) {
            new AlertDialog.Builder(Preferences.this)
                    .setTitle("Are you sure to clear history")
                    .setPositiveButton("Ok", new OnClickListener() {   
                        public void onClick(DialogInterface arg0, int arg1) {
                        	
                        	suggestions.clearHistory();
                        	Toast.makeText(Preferences.this, "Search history is cleared for this app", Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton("No", new  DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();
							
						}
                    })
				 .show();
        } else if (key.equals("about")) {
            /*
             * Uri uri = Uri.parse("www.baidu.com"); startActivity(new
             * Intent(Preferences.this, uri));
             */
        }
        return true;
    }

}
